/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.experimentation;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import co.edu.unal.bioingenium.kbmed.knowledge.KnowledgeSourceMetaData;
import co.edu.unal.bioingenium.kbmed.knowledge.dao.KnowledgeSourceDAO;
import co.edu.unal.bioingenium.kbmed.knowledge.dao.KnowledgeSourceDAOFactory;
import co.edu.unal.bioingenium.kbmed.knowledge.snomed.SnomedMetaData;
import co.edu.unal.bioingenium.kbmed.retrieval.text.process.PrepareByWords;
import co.edu.unal.bioingenium.kbmed.text.representation.io.CorpusIO;
import co.edu.unal.bioingenium.kbmed.text.representation.vo.Corpus;
import co.edu.unal.bioingenium.kbmed.util.io.FileManager;
import co.edu.unal.bioingenium.kbmed.util.time.TimeUtil;
import gnu.trove.map.TObjectIntMap;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class KnowledgeSourceStatistics {

//    private String[] snomedCategories = {
//        "disorder",
//        "procedure",
//        "finding",
//        "organism",
//        "body structure",
//        "substance",
//        "product"};
    private final String startDate;

    public KnowledgeSourceStatistics() {
        startDate = TimeUtil.getCurrentDate();
    }

    public void extractStatistics() {
        KnowledgeSourceDAO knowledgeSourceDAO = KnowledgeSourceDAOFactory.getKnowledgeSourceDAO();
        PrepareByWords prepareCollection = new PrepareByWords();
        Corpus corpus;
        Set<String> conceptsIds;
        Map<String, Set<String>> termsByconceptIds = knowledgeSourceDAO.getTermsByConceptId();
        Set<String> terms;
        StringBuilder sb = new StringBuilder();
        Map<String, String> corpusData;
        Set<String> categories = knowledgeSourceDAO.getCategories();
        for (String category : categories) {
            Logger.getLogger(KnowledgeSourceStatistics.class.getName()).log(Level.INFO, "Working in category {0}", category);
            corpusData = new HashMap<String, String>();
            conceptsIds = knowledgeSourceDAO.getConceptsInCategory(category);
            for (String conceptId : conceptsIds) {
                sb.setLength(0);
                terms = termsByconceptIds.get(conceptId);
                for (String term : terms) {
                    sb.append(term).append(" ");
                }
                corpusData.put(conceptId, sb.toString());
            }
            corpus = prepareCollection.buildCorpus(category, corpusData, 0, 0);
            CorpusIO.storeCorpus(corpus, category + ".dat");
            saveCorpusStatistics(category, corpus);
        }
    }

    public void saveCorpusStatistics(String category, Corpus corpus) {
        BufferedWriter bufferedWriter = null;
        try {
            String savePath = Configuration.EXPERIMENTS_DIR + File.separator + KnowledgeSourceStatistics.class.getSimpleName() + File.separator + startDate;
            FileManager.verifyAndCreatePath(savePath);
            String categoryStandard = FileManager.getStandardName(category);
            bufferedWriter = new BufferedWriter(new FileWriter(new File(FileManager.toStandardPath(savePath) + Configuration.KNOWLEDGE_SOURCE_NAME + "-" + categoryStandard + "Statistics.csv")));
            bufferedWriter.write("Total_documents " + corpus.getTotalDocuments() + "\n");
            bufferedWriter.write("Average_document_length " + corpus.getAverageDocumentLength() + "\n");
            bufferedWriter.write("Total_words " + corpus.getTotalWords() + "\n");
            TObjectIntMap<String> wordList = corpus.getWordCount();
            bufferedWriter.write("word,totalOcurrences,inDocumentOccurrences\n");
            StringBuilder sb = new StringBuilder();
            for (String word : wordList.keySet()) {
                sb.setLength(0);
                sb.append(word).append(",").append(wordList.get(word)).append(",").append(corpus.getDocumentOcurrences(word)).append("\n");
                bufferedWriter.write(sb.toString());
            }
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (IOException ex) {
            Logger.getLogger(KnowledgeSourceStatistics.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(KnowledgeSourceStatistics.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        KnowledgeSourceStatistics knowledgeSourceStatistics = new KnowledgeSourceStatistics();
        knowledgeSourceStatistics.extractStatistics();
    }
}
